<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Controllers\Controller;
use App\Models\Admin\AdminProduct;
use App\Models\Admin\ServiceApp;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class ServiceAppController extends AdminController
{
    use AdminTrait;

    protected $model;
    protected $unique_id = '';

    protected $app_type = [
        1 => '系统应用',
        2 => '第三方应用',
    ];

    protected $charge_type = [
        1 => '免费',
        2 => '收费',
    ];

    protected $open_type = [
        1 => '当前窗口',
        2 => '新建窗口',
        3 => '二维码'
    ];

    protected $validate_rule = [
        'open_type' => 'required|min::1|max:2',
        'app_type' => 'required|min::1|max:2',
        'charge_type' => 'required|min::1|max:2',
    ];

    protected function rules()
    {
        return [
            'name' => [
                'required',
                Rule::unique('admin_service_app')->ignore($this->unique_id),
            ],
        ];
    }

    protected $err_msg = [
        'name.required' => '应用名称必须',
        'name.unique' => '应用名称必须唯一',
        'open_type.required' => '打开方式必选',
        'open_type.min' => '打开方式最小值为1',
        'open_type.max' => '打开方式最大值为2',
        'app_type.required' => '应用类型必选',
        'app_type.min' => '应用类型最小值为1',
        'app_type.max' => '应用类型最大值为2',
        'charge_type.required' => '收费方式必选',
        'charge_type.min' => '收费方式最小值为1',
        'charge_type.max' => '收费方式最大值为2',
    ];

    public function __construct(ServiceApp $model)
    {
        $this->model = $model;
    }

    protected function beforeIndex()
    {
        $product = AdminProduct::pluck('name','id');
        return ['product'=>$product,'app_type_list'=>$this->app_type,'charge_type_list'=>$this->charge_type,'open_type_list'=>$this->open_type];
    }

    protected function search($request)
    {

        $select = $this->model;
        ($app_type = $request->query('app_type')) && $select = $select->where('app_type',$app_type);
        ($charge_type = $request->query('charge_type')) && $select = $select->where('charge_type',$charge_type);
        return $select;
    }

    protected function setRequestField($arr,$type=false)
    {
        //过滤字段
        if (isset($arr['sign'])) unset($arr['sign']);
        if (isset($arr['timeStamp'])) unset($arr['timeStamp']);
        return $arr;
    }
}
